package com.realbrand.net.realbrandpay.ui.activity.wallet.personer.adress;

import com.realbrand.net.realbrandpay.api.HTTPAPI;
import com.realbrand.net.realbrandpay.bean.AdressBean;
import com.realbrand.net.realbrandpay.inter.IAddAddressInfo;
import com.realbrand.net.realbrandpay.inter.IDelAddressInfo;
import com.realbrand.net.realbrandpay.inter.ISelectAddressInfo;
import com.realbrand.net.realbrandpay.inter.IUpdateAddressInfo;
import com.realbrand.net.realbrandpay.inter.IUpdateDefaultAddress;
import com.realbrand.net.realbrandpay.ui.activity.wallet.personer.adress.IModelManagerAdress;
import com.realbrand.net.realbrandpay.untils.HttpRequest;
import com.realbrand.net.realbrandpay.zxing.util.L;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/5/8.
 */

public class ModelManagerAdress {
    /**
     * 进行网络请求，获取所有地址
     *
     * @param param               参数
     * @param iModelManagerAdress 接口回调
     */
    public static void getData(String param, final IModelManagerAdress<List<AdressBean>> iModelManagerAdress) {
        RequestBody body = RequestBody.create(HTTPAPI.JSON, param);
        final List<AdressBean> data = new ArrayList<AdressBean>();
//        ISelectAddressInfo iSelectAddressInfo = HttpRequest.request_BASE_URL(ISelectAddressInfo.class);
//        Call<ResponseBody> adress = iSelectAddressInfo.getAdress(body);
//        adress.enqueue(new Callback<ResponseBody>() {
//            @Override
//            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//                try {
//                    L.d(L.TAG, "查询地址返回的数据" + response.body().string());
//
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//
//            @Override
//            public void onFailure(Call<ResponseBody> call, Throwable t) {
//
//            }
//        });
//        iModelManagerAdress.onComplete();
        ISelectAddressInfo iSelectAddressInfo = HttpRequest.request_BASE_URL_VERSION(ISelectAddressInfo.class, HTTPAPI.BASE_URL);
        iSelectAddressInfo.getAdress(body)
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<AdressBean>() {
                    @Override
                    public void onCompleted() {
                        iModelManagerAdress.onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        iModelManagerAdress.onErrorr();
                        iModelManagerAdress.onFail(e.toString());
                        L.d(L.TAG, "异常" + e.toString());
                    }

                    @Override
                    public void onNext(AdressBean adressBean) {
                        L.d(L.TAG, "查询地址返回的Bean" + adressBean.toString());
                        if (adressBean.getRETURNCODE().equals("204")) {

                        } else {
                            for (int i = 0; i < adressBean.getAddressId().size(); i++) {
                                data.add(adressBean);
                            }
                        }
                        iModelManagerAdress.onSuccess(data);

                    }
                });
    }

    /**
     * 添加地址
     *
     * @param param
     * @param iModelManagerAdress
     */
    public static void addAdress(String param, final IModelManagerAdress<String> iModelManagerAdress) {
        RequestBody body = RequestBody.create(HTTPAPI.JSON, param);
        IAddAddressInfo iAddAddressInfo = HttpRequest.request_BASE_URL(IAddAddressInfo.class);
        iAddAddressInfo.addAdress(body).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String addAdressResult = response.body().string();
                    L.d(L.TAG, "添加地址返回数据" + addAdressResult.toString());
                    iModelManagerAdress.onSuccess(addAdressResult);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                iModelManagerAdress.onFail(t.toString());
            }
        });
    }

    /**
     * 删除地址
     *
     * @param params
     * @param stringIModelManagerAdress
     */
    public static void deleteAdress(String params, final IModelManagerAdress<String> stringIModelManagerAdress) {
        RequestBody body = RequestBody.create(HTTPAPI.JSON, params);
        IDelAddressInfo iDelAddressInfo = HttpRequest.request_BASE_URL(IDelAddressInfo.class);
        iDelAddressInfo.deleteAdress(body).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String deleteAdressResult = response.body().string();
                    L.d(L.TAG, "删除地址返回的数据" + deleteAdressResult.toString());
                    stringIModelManagerAdress.onSuccess(deleteAdressResult);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                stringIModelManagerAdress.onFail(t.toString());
            }
        });
    }

    /**
     * 设置默认地址
     *
     * @param params
     * @param idefaultAdress
     */
    public static void setDefaultAddress(String params, final IModelManagerAdress<String> idefaultAdress) {
        RequestBody body = RequestBody.create(HTTPAPI.JSON, params);
        IUpdateDefaultAddress iDelAddressInfo = HttpRequest.request_BASE_URL(IUpdateDefaultAddress.class);
        iDelAddressInfo.setDefaultAdress(body).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String setDefaultAddressResult = response.body().string();
                    L.d(L.TAG, "设置默认地址返回的结果" + setDefaultAddressResult.toString());
                    JSONObject jsonObject = new JSONObject(setDefaultAddressResult);
                    String returncode = jsonObject.getString("RETURNCODE");
//                    String setDefault;
//                    if (returncode.equals("200")) {
//                        setDefault = "设置默认地址成功";
//                    } else {
//                        setDefault = "设置默认地址失败";
//                    }
                    idefaultAdress.onSuccess(setDefaultAddressResult);
                } catch (Exception e) {
                    e.printStackTrace();
                    L.d(L.TAG, "设置默认地址返回的结果异常信息" + e.toString());
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                idefaultAdress.onFail(t.toString());
            }
        });
    }

    /**
     * 更新地址信息
     *
     * @param param
     * @param upDataAdress
     */
    public static void upDataAddress(String param, final IModelManagerAdress<String> upDataAdress) {
        RequestBody body = RequestBody.create(HTTPAPI.JSON, param);
        IUpdateAddressInfo iUpdateAddressInfo = HttpRequest.request_BASE_URL(IUpdateAddressInfo.class);
        iUpdateAddressInfo.upDataAdress(body).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String upDataAddressResult = response.body().string();
                    upDataAdress.onSuccess(upDataAddressResult);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                upDataAdress.onFail(t.toString());
            }
        });
    }

}
